<?php
namespace Admin\Controller;
use Think\Controller;

class BaseController extends Controller {

	protected function _initialize() {
		if(!$this->hasAuth()) {
			$this->error('你没有权限，请登陆或者向管理员申请！', U('Login/login'));
		}
	}

	protected function hasAuth() {
		$role = I('session.role');
		$rules = array();
		switch ($role) {
			case C('ROLE_SUPERVISOR'):
				$rules = C('RULES_SUPERVISOR');
				break;
			case C('ROLE_ADMINISTRATOR'):
				$rules = C('RULES_ADMINISTRATOR');
				break;
			case C('ROLE_TEACHER'):
				$rules = C('RULES_TEACHER');
				break;
			case C('ROLE_STUDENT'):
				$rules = C('RULES_STUDENT');
				break;
		}
		$flag = false;
		foreach ($rules as $rule) {
			if(in_array(CONTROLLER_NAME, $rule)) {
				$flag = true;
				break;
			}
		}
		return $flag;
	}

}
